package com.test.www.util;

import com.test.www.entity.Admin;
import com.test.www.entity.Student;

import javax.swing.*;
import java.io.*;

/**
 * <p>文件工具类</p>
 * @author Bosen
 * 2020/12/16 21:55
 */
public class FileUtil {
    /*
     * 单例模式
     */
    public static FileUtil fileUtil = new FileUtil();

    public static FileUtil getInstance(){
        return fileUtil;
    }

    /*
     * 获取管理员信息
     */
    public IArrayList<Admin> getAdmin() throws IOException {
        // jar运行
        File adminTxt = new File(".","/data/admin.txt");
        // 本地运行
        //File adminTxt = new File("..","/data/admin.txt");

        InputStreamReader isr = new InputStreamReader(new FileInputStream(adminTxt),"UTF-8");
        BufferedReader reader = new BufferedReader(isr);

        IArrayList<Admin> list = new IArrayList<>();
        String adminInfo = null;
        while ((adminInfo=reader.readLine())!=null){
            String[] array = adminInfo.split(",");
            String username = array[0];
            String password = array[1];
            
            Admin admin = new Admin(username,password);
            list.add(admin);
        }
        return list;
    }

    /*
     * 获取所有学生信息
     */
    public IArrayList<Student> getStudent() throws IOException {
        // jar运行
        File studentTxt = new File(".","/data/student.txt");
        // 本地运行
        //File studentTxt = new File("..","/data/student.txt");

        InputStreamReader isr = new InputStreamReader(new FileInputStream(studentTxt),"UTF-8");
        BufferedReader reader = new BufferedReader(isr);

        IArrayList<Student> list = new IArrayList<>();
        String studentInfo = null;
        while ((studentInfo=reader.readLine())!=null){
            String[] array = studentInfo.split(",");
            String id = array[0];
            String username = array[1];
            double math = Double.valueOf(array[2]);
            double chinese = Double.valueOf(array[3]);
            double english = Double.valueOf(array[4]);
            double physics = Double.valueOf(array[5]);
            double computer = Double.valueOf(array[6]);

            Student student = new Student(id,username,math,chinese,english,physics,computer);
            list.add(student);
        }
        return list;
    }
    
    /*
     * 录入学生信息
     */
    public void addStudent(String id, String username, double math, double chinese, double english, double physics, double computer) throws IOException {
        // jar运行
        File studentTxt = new File(".","/data/student.txt");
        // 本地运行
        //File studentTxt = new File("..","/data/student.txt");

        BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (studentTxt,true),"UTF-8"));

        PrintWriter printWriter = new PrintWriter(writer);
        printWriter.println(id+","+username+","+math+","+chinese+","+english+","+physics+","+computer);

        printWriter.flush();
        printWriter.close();
    }
    
    /**
     * <p>更新学生信息</p>
     * @author Bosen
     *
     * 2020/12/18 14:37
     */
    public void updateStudent(JTable jTable) throws IOException {
        IArrayList<Student> list = new IArrayList<>();
        // 更新学生信息
        for (int i = 0; i < jTable.getRowCount(); i++){
            String id = jTable.getValueAt(i,1).toString();
            String username = jTable.getValueAt(i,2).toString();
            double math = Double.valueOf(jTable.getValueAt(i,3).toString());
            double chinese = Double.valueOf(jTable.getValueAt(i,4).toString());
            double english = Double.valueOf(jTable.getValueAt(i,5).toString());
            double physics = Double.valueOf(jTable.getValueAt(i,6).toString());
            double computer = Double.valueOf(jTable.getValueAt(i,7).toString());
            Student student = null;
            // 修改学生信息
            student = new Student(id,username,math,chinese,english,physics,computer);
            list.add(student);
        }

        // 将更新后的信息覆盖原有信息
        // jar运行
        File studentTxt = new File(".","/data/student.txt");
        // 本地运行
        //File studentTxt = new File("..","/data/student.txt");

        BufferedWriter writer1 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (studentTxt),"UTF-8"));
        writer1.write("");
        BufferedWriter writer2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (studentTxt),"UTF-8"));        PrintWriter printWriter = new PrintWriter(writer2);
        for (Student student : list) {
            String tempString = student.getId()+","+student.getUsername()+","+student.getMath()+","+student.getChinese()+","+student.getEnglish()+","+student.getPhysics()+","+student.getComputer();
            printWriter.println(tempString);
        }

        printWriter.flush();
        printWriter.close();
    }

    /**
     * <p>删除学生信息</p>
     * @author Bosen
     *
     * 2020/12/18 14:37
     */
    public void deleteStudent(int row,JTable jTable) throws IOException {
        IArrayList<Student> list = new IArrayList<>();
        // 更新学生信息
        for (int i = 0; i < jTable.getRowCount(); i++){
            if (i != row){
                String id = jTable.getValueAt(i,1).toString();
                String username = jTable.getValueAt(i,2).toString();
                double math = Double.valueOf(jTable.getValueAt(i,3).toString());
                double chinese = Double.valueOf(jTable.getValueAt(i,4).toString());
                double english = Double.valueOf(jTable.getValueAt(i,5).toString());
                double physics = Double.valueOf(jTable.getValueAt(i,6).toString());
                double computer = Double.valueOf(jTable.getValueAt(i,7).toString());
                Student student = null;
                // 修改学生信息
                student = new Student(id,username,math,chinese,english,physics,computer);
                list.add(student);
            }
        }

        // 将更新后的信息覆盖原有信息
        // jar运行
        File studentTxt = new File(".","/data/student.txt");
        // 本地运行
        //File studentTxt = new File("..","/data/student.txt");

        BufferedWriter writer1 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (studentTxt),"UTF-8"));
        writer1.write("");
        BufferedWriter writer2 = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (studentTxt),"UTF-8"));
        PrintWriter printWriter = new PrintWriter(writer2);
        for (Student student : list) {
            String tempString = student.getId()+","+student.getUsername()+","+student.getMath()+","+student.getChinese()+","+student.getEnglish()+","+student.getPhysics()+","+student.getComputer();
            printWriter.println(tempString);
        }

        printWriter.flush();
        printWriter.close();
    }
}
